#include "registereduserinformation.h"
#include "ui_registereduserinformation.h"
#include<QDebug>

Q_GLOBAL_STATIC(RegisteredUserInformation,theInstance);
     RegisteredUserInformation::RegisteredUserInformation(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RegisteredUserInformation)
{
    ui->setupUi(this);
    this->setWindowTitle("注册界面");
    this->setWindowIcon(QIcon(":res/res/Register.ico"));
    //设置背景
    this->setFixedSize(1587,800);
    this->setStyleSheet(
        "RegisteredUserInformation {"
        "   background-image: url(:/res/res/register_p.png);" // 资源路径
        "   background-position: center;"                   // 居中
        "   background-repeat: no-repeat;"                  // 不重复
        "   background-attachment: fixed;"                  // 固定不滚动
        "   background-size: cover;"                        // 自适应填充
        "}"

        "QLabel, QPushButton {"
        "   background: white;"                       // 子控件为白色
        "}"

    );
    this->setFixedSize(1587,800);
    QRegExp regExp("\\d{6}");//限制输入六位数字密码

    QRegExp regExpA("\\d{11}");//限制输入11位账号
    QRegExpValidator *validator = new QRegExpValidator(regExp, this);
    QRegExpValidator *validatorA = new QRegExpValidator(regExpA, this);
    ui->lineEditPassword->setValidator(validator);
    ui->lineEditAccountNumber->setValidator(validatorA);
    //设置输入框的显示模式为密码模式
    ui->lineEditPassword->setEchoMode(QLineEdit::Password);
    //限制管理员类型输入为0或1
    QRegExp regExp2("^[01]$");
    QRegExpValidator *validator2 = new QRegExpValidator(regExp2, this);
    ui->lineEditUserType->setValidator(validator2);
    setQLineEdit();//设置输入框样式
    //设置按钮为可选中的状态
//    ui->pushButtonHOS->setCheckable(true);
//    //初始化按钮选中状态
    ui->pushButtonHOS->setChecked(false);//初始化选中为false
    status=ui->pushButtonHOS->isChecked();
    ui->pushButtonHOS->setText("");
    ui->pushButtonHOS->setStyleSheet(
              "QPushButton { background-color:red ;border: 3px solid rgb(0, 170, 255); border-radius: 20px; padding: 5px 15px;  }"
              ); // 选中样式
//connect(userMysql::instance(),userMysql::clearRegisterInformation,this,RegisteredUserInformation::clearLineEdit,Qt::QueuedConnection);
displayAdminCount();
}

RegisteredUserInformation::~RegisteredUserInformation()
{
    delete ui;
}

RegisteredUserInformation *RegisteredUserInformation::instance()
{
    return theInstance;
}

void RegisteredUserInformation::ClearLineEdit()
{
clearLineEdit();
}

void RegisteredUserInformation::on_pushButton_confirm_clicked()
{

bool ok=saveUserIformation();
if(ok)
{
emit RegisterUser();
    qDebug()<<"send RegisterUser";
}
//更新UserInformation
}


void RegisteredUserInformation::on_pushButton_Cancel_clicked()
{
clearLineEdit();

this->close();

}

bool RegisteredUserInformation::saveUserIformation()//saveUserIformation(UserInfo &user)
{

  UserInfo demo;
  demo.accountNumber=ui->lineEditAccountNumber->text();
  demo.userName=ui->lineEditUserName->text();
  demo.userType=ui->lineEditUserType->text().toInt();
  demo.userPassword=ui->lineEditPassword->text();
//QMessageBox
  qDebug()<<demo.accountNumber<<demo.userName<<" "<<demo.userType<<" "<<demo.userPassword<<endl;

 //bool ok= userMysql::instance()->saveUserData(demo);
  //在注册用户信息到数据库之前首先判断管理员账号数量是否已达到上限5
  if(demo.userType)
  {
   if(getAdminCount()>=5)
   {
       QMessageBox ::warning(this,"warning","管理员账号达到上限");
       return false;
   }

  }
  bool ok= UserMysqlS::instance()->saveUserData(demo);//保存用户信息到数据库

 if(ok)
 {
   emit upUserData(ok);
   if(demo.userType)
   {
       displayAdminCount();//更新管理员账号数量；
   }



 }
 return ok;

}

void RegisteredUserInformation::clearLineEdit()
{
    ui->lineEditAccountNumber->clear();
    ui->lineEditUserName->clear();
    ui->lineEditUserType->clear();
    ui->lineEditPassword->clear();



}

void RegisteredUserInformation::hidenOrShowPassword(bool key)
{
if(key==true){
    ui->pushButtonHOS->setStyleSheet(
              "QPushButton { background-color:rgb(0,255,255); border: 3px solid rgb(0, 170, 255); border-radius: 20px; padding: 5px 15px; }"

              ); // 选中样式background-image: url(:/res/res/dispalyPassword.png)

    ui->pushButtonHOS->setText("");
//    ui->pushButtonHOS->setText("显示");
    ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
}
else
{
    ui->pushButtonHOS->setStyleSheet(
              "QPushButton { background-color:red ;border: 3px solid rgb(0, 170, 255); border-radius: 20px; padding: 5px 15px; }"
            ); // 选中样式
    ui->pushButtonHOS->setText("");
//    ui->pushButtonHOS->setText("隐藏");
    ui->lineEditPassword->setEchoMode(QLineEdit::Password);

}
}

int RegisteredUserInformation::getAdminCount()
{
    return UserMysqlS::instance()->getAdminCount();
}

void RegisteredUserInformation::displayAdminCount()
{
    ui->lcdNumberAdminCount->display(getAdminCount());
}

void RegisteredUserInformation::setQLineEdit()
{
    // 边框颜色和粗细
    // 圆角半径
    // 内边距（上下 6px，左右 12px）
    // 字体大小
    // 背景颜色
    // 文字颜色
    // 获得焦点时的样式
    QString style1="QLineEdit {"
                   "   border: 2px solid #3498db;"
            "   border-radius: 8px;"
            "   padding: 6px 12px;"
            "font: 24pt 'Microsoft JhengHei UI';"
            "   font-size: 20px;"
            "   background: #f8f9fa;"
            "   color: #2c3e50;"
            "}"
            "QLineEdit:focus {"
            "   border: 2px solid #2980b9;"
            "   background: #ffffff;"
            "}";


    // 设置基础样式
    ui->lineEditAccountNumber->setStyleSheet(style1);
    ui->lineEditUserName->setStyleSheet(style1);
    ui->lineEditUserType->setStyleSheet(style1);
    ui->lineEditPassword->setStyleSheet(style1);
}


void RegisteredUserInformation::on_pushButtonHOS_clicked()
{

 if(status==false)
 {


  ui->pushButtonHOS->setChecked(true);
  status=true;
 }
 else{
     ui->pushButtonHOS->setChecked(false);
     status=false;
 }
 hidenOrShowPassword(status);
}

